import GameModel from "../Model/GameModel";
import { ANITIME,SCORE,BETCONFIG } from "../Model/ConstValue";

cc.Class({
    extends: cc.Component,

    properties: {
        grid:{
            default: null,
            type: cc.Node
        },
        operation:{
            default:null,
            type:cc.Node
        },
        option:{
            default:null,
            type:cc.Node
        }
    },
    onLoad: function () {
        cc.director.setDisplayStats(false);
        this.init();
    },
    init:function()
    {
        this.optLayer = this.option.getComponent("OptionLayer");        
        this.gridScript = this.grid.getComponent("GridView");
        this.operateLayer = this.operation.getComponent("OperationLayer");
        this.optLayer.setController(this);
        this.gridScript.setController(this);
        this.operateLayer.setController(this);
    },
    startGame:function()
    {
        var time = ANITIME.DOWN
        this._doCursh = false;
        this.cleanPool();
        this.cleanRecord();
        this.gameModel = new GameModel();
        this.gameModel.init(5);
        this.gameModel.setCurBetCount(this.operateLayer.getCurBet());

        time += this.gridScript.initWithCellModels(this.gameModel.getCells());
        this.cleanCmd();
        this._scheduler = new (require('common').MyScheduler)();
        this.play(time);
    },
    autoPlay:function(auto)
    {
        this._autoPlay = auto;
        if(!this._gamePlaying)
        {
            this.startGame();
        }
    },
    play: function (dt = 0.5) {
        this._gamePlaying = true;
        this._scheduler.schedule(this.firstCheck,0, 1, dt, this, 'animate');
    },
    firstCheck(){
        var result = this.gameModel.checkCrush();
        
        this.executeCrush(result);
    },
    //执行消除
    executeCrush :function(result)
    {
        // var scoreTotal = 
        this.gridScript.executeCrush(result);
        //this.gameModel.addScore(scoreTotal);
        this.crushDone(result[3],result[2]);
    },
    //游戏结束
    gameEnd:function()
    {
        
    },
    //消除完成后
    crushDone:function(length,time)
    {
        if(length > 0)
        {
            this._doCursh = true;
            this.play(time);
        }else{
            //this.gameEnd();
            if(this._autoPlay)
            {
                this.startGame();
            }else{
                this.operateLayer.recoverStartBtn(true);
                this._gamePlaying = false;
            }
        }
    },
    getCurBetScore:function(type,count)
    {
        var score = 0;
        for(var i = 0;i < SCORE.length;i++)
        {
            if(type == SCORE[i].type)
            {
                for(var j = 0;j < SCORE[i].score.length;j++)
                {
                    if(count == SCORE[i].score[j].count)
                    {
                        score = SCORE[i].score[j].scale;
                    }
                }
            }
        }
        return score * this.getCurBetCount();
    },
    getCurBetCount:function()
    {
        if(this.gameModel)
        {
            return this.gameModel.getCurBetCount()
        }else{
            return 100;
        }
    },
    setBetCount:function(count)
    {
        this.gameModel.setCurBetCount(count)
    },
    selectCell: function(pos){
        return this.gameModel.selectCell(pos);
    },
    cleanCmd: function(){
        this.gameModel.cleanCmd();
    },
    cleanPool:function()
    {
        return this.gridScript.cleanPool();
    },
    cleanRecord:function()
    {
        return this.gridScript.cleanRecord();
    }
});
